﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;

namespace Ewts.Storage
{
    public partial class InStorage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    if (curUser.UserType == "AD")
                        this.hidgraphic.Value = "";
                    else
                    {
                        this.hidgraphic.Value = curUser.CompanyCity;
                    }
                    HiddenOper.Value = curUser.UserName;
                    SetDisplay();
                    hidUserName.Value = curUser.UserName;
                    bindMonth();
                    dataBind();
                }



            }


        }

        protected void SetDisplay()
        {

            ///从出库单过来
            if (Request["outid"] != null && Request["outid"].ToString().Trim() != "")
            {
                BindOutid(Request["outid"].ToString(), null);
            }
            else if (Request["insid"] != null && Request["insid"].ToString().Trim() != "")
            {
                DataLib.Model.InStorage insModel = new DataLib.BLL.InStorage().GetModel(int.Parse(Request["insid"].ToString().Trim()));
                hidInsid.Value = insModel.id.ToString();
                if (insModel.outid == null || insModel.outid == 0)
                {
                    bindZXInfo((int)insModel.cjoid);
                }
                else
                {
                    if (insModel.clearingmonth != null && insModel.clearingmonth.Length == 6)
                    {
                        txtRemark.InnerHtml = insModel.remark;
                       
                        string stryear = insModel.clearingmonth.Substring(0, 4);
                        string strmonth = insModel.clearingmonth.Substring(4, 2);
                        dropYear.SelectedValue = stryear;
                        dropMonth.SelectedValue = strmonth;
                    }
                    if (HiddenOper.Value == "admin" || txtRemark.Value.Trim() == "")
                    {
                        txtRemark.Disabled = false;
                    }
                    else
                    {
                        txtRemark.Disabled = true;
                    }
                    BindOutid(insModel.outid.ToString(), insModel);
                }
            }
            else if(Request["cjoid"]!=null && Request["cjoid"].ToString().Trim()!="")   //直销单
            {
                bindZXInfo(int.Parse(Request["cjoid"].ToString().Trim()));

            }

        }

        /// <summary>
        /// 第一次入库创建入库编号
        /// </summary>
        /// <param name="outModel"></param>
        /// <returns></returns>
        protected string GetSerialNum(string outtype)
        {
            string strNum = "";
            string strInLeft = "R";
            switch (outtype)
            {
                case "2":
                    strInLeft += "F";
                    HidStatus.Value = "3";
                    break;
                case "1":

                    strInLeft += "C";
                    HidStatus.Value = "5";
                    break;
                case "3":

                    strInLeft += "S";
                    HidStatus.Value = "5";
                    break;
                case "4":

                    strInLeft += "X";
                    HidStatus.Value = "7";
                    break;
                case "5":

                    strInLeft += "Y";
                    HidStatus.Value = "9";
                    break;
                case "7":
                    strInLeft += "Z";
                    HidStatus.Value = "13";
                    break;
                default:
                    break;
            }
            hidInstype.Value = strInLeft;
            //strNum = Code.NumGeneration.GetSerialNum(strInLeft, "InCode");

            return strNum;
        }

        /// <summary>
        /// 获取BarCode的是否锁定状态
        /// </summary>
        /// <param name="islocak"></param>
        /// <returns></returns>
        protected string GetIsLock(string islocak)
        {
            if (Boolean.Parse(islocak))
                return "checked";
            else
                return "";
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        /// <param name="loadID"></param>
        protected void dataBind()
        {
            //根据出库ID取得入库ID
            string outid = hidoutparID.Value;
            DataLib.BLL.BarCode bllBarCode = new DataLib.BLL.BarCode();
            int instroingid = int.Parse(hidInStoID.Value);

            if (instroingid != 0)
            {

                hidInsid.Value = instroingid.ToString();
                //处理前 分类汇总的数据绑定
                DataTable dtun = new DataTable();
                dtun = new DataLib.BLL.unProcessed().GetList("inSid=" + instroingid.ToString()).Tables[0];
                reptun.DataSource = dtun;
                reptun.DataBind();

                //入库数据绑定
                DataTable dtbar = new DataTable();
                dtbar = new DataLib.BLL.BarCode().GetList("InStrorageID=" + instroingid.ToString().Trim()).Tables[0];
                reptin.DataSource = dtbar;
                reptin.DataBind();
            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (hidInStoID.Value.Trim() == "" || hidInStoID.Value.Trim() == "0")
            {
                DataLib.Common.MessageBox.Show(this.Page, "不要提交空数据");
                return;
            }
            string insid = hidInStoID.Value;
            DataLib.Model.InStorage insModel = new DataLib.BLL.InStorage().GetModel(int.Parse(insid));
            int outid = (int)insModel.outid;

            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            DataLib.BLL.OutStorage bllOut = new DataLib.BLL.OutStorage();
            outModel = bllOut.GetModel(outid);
            string outtype = "0";
            string month = dropYear.SelectedValue + dropMonth.SelectedValue;

            DataLib.BLL.InStorage bllIn = new DataLib.BLL.InStorage();
            if (outModel != null)
            {
                outtype = outModel.outStorageType;
            }
            else
            {
                outtype = "7";
            }
            bllIn.InstorageSubmit(int.Parse(insid), outtype,month);


            switch (outtype)
            {
                case "1":
                case "2":
                case "7":
                    DataLib.Common.MessageBox.ShowNewWindowUrl(Page, "/RDLC/SegregationRDLC.aspx?instorageID=" + hidInStoID.Value);
                    break;
                case "3":
                    DataLib.Common.MessageBox.ShowAndRedirect(Page, "提交成功，请选择打印", "/Storage/SelectReport.aspx?insid=" + hidInStoID.Value);
                    break;
                case "5":
                    DataLib.Common.MessageBox.ShowNewWindowUrl(Page, "/RDLC/SegregationRDLC.aspx?instorageID=" + hidInStoID.Value);
                    break;
                default:
                    break;
            }


            DataLib.Common.MessageBox.ShowAndRedirect(Page, "保存成功！", "/Storage/WaitInStorageList.aspx");

        }




        /// <summary>
        /// 通过库位号找到库位ID
        /// </summary>
        /// <param name="storCode">库位号</param>
        /// <returns>库位ID</returns>
        protected int? GetStorPosOption(string storCode)
        {
            DataLib.Model.StoragePostion storPosM = new DataLib.BLL.StoragePostion().GetModelByCode(storCode);

            return storPosM.id;
        }


        /// <summary>
        /// 根据cjoid对派车状态进行改变
        /// </summary>
        /// <param name="cjoid">CJO单的ID号</param>
        protected void SetVehicleArrange(int cjoid)
        {
            DataLib.Model.VehicleArrange vamodel = new DataLib.BLL.VehicleArrange().GetModelByCjoid(cjoid);

            //派车操作
            if (vamodel != null && vamodel.status == 1)
            {
                vamodel.status = 0;

                string[] arrplate = vamodel.platenumberid.Split(',');
                int i = 0;
                for (i = 0; i < arrplate.Length; i++)
                {
                    if (arrplate[i] != null && arrplate[i].ToString().Trim() != "")
                    {
                        int plateid = int.Parse(arrplate[i]);
                        DataLib.Model.PlateNumber platemodel = new DataLib.BLL.PlateNumber().GetModel(plateid);
                        platemodel.currentstatus--;
                        new DataLib.BLL.PlateNumber().Update(platemodel);
                    }
                }

                //收货员操作
                string[] arrvehider = vamodel.vehiderid.Split(',');
                for (i = 0; i < arrvehider.Length; i++)
                {
                    if (arrvehider[i] != null && arrvehider[i].ToString().Trim() != "")
                    {
                        int vehiderid = int.Parse(arrvehider[i]);
                        DataLib.Model.Vehiclor vehmodel = new DataLib.BLL.Vehiclor().GetModel(vehiderid);
                        vehmodel.currentstatus--;
                        new DataLib.BLL.Vehiclor().Update(vehmodel);
                    }
                }
            }
        }

        protected void btnCanel_Click(object sender, EventArgs e)
        {
            DataLib.Common.MessageBox.ShowAndRedirect(Page, "跳转测试", "/Storage/SelectReport.aspx?insid=" + hidInStoID.Value);

        }

        private void BindOutid(string outid, DataLib.Model.InStorage imodel)
        {
            DataLib.Model.InStorage inModel = new DataLib.Model.InStorage();
            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            DataLib.Model.LoadNum loadModel = new DataLib.Model.LoadNum();

            DataLib.BLL.InStorage inBll = new DataLib.BLL.InStorage();
            DataLib.BLL.BarCode barBll = new DataLib.BLL.BarCode();
            DataLib.BLL.OutStorage outBll = new DataLib.BLL.OutStorage();
            DataLib.BLL.LoadNum loadBll = new DataLib.BLL.LoadNum();

            //  if(imodel != null)

            hidoutparID.Value = outid;
            outModel = outBll.GetModel(int.Parse(outid));
            DataTable inDt = inBll.GetList("outid=" + outid).Tables[0];
            //需要从出库单带出来信息
            inputLoadNum.Value = outModel.VLoadNum;
            HidVLoad.Value = outModel.VLoadNum;
            hidoutparID.Value = outModel.id.ToString();
            txtSrcOutSid.Value = outModel.outStorageNo;
            hidInDepart.Value = outModel.belogarea;
            hidOutDepart.Value = outModel.graphicCity;
            switch (outModel.outStorageType)
            {
                case "1":
                    hidintype.Value = "C";
                    break;
                case "2":
                    hidintype.Value = "F";
                    break;
                case "3":
                    hidintype.Value = "S";
                    break;
                case "5":
                    hidintype.Value = "Y";
                    break;
                default:
                    hidintype.Value = "D";
                    break;
            }

            if (outModel.VLoadNum.IndexOf("86") == 0)
            {
                //分类入库的时候
                loadModel = loadBll.GetLoadID(outModel.VLoadNum);

                txtcutomer.Value = loadModel.cumname;
                hidLoadID.Value = loadModel.id.ToString();

            }
            else
            {
                DataLib.Model.Vload vmodel = new DataLib.Model.Vload();
                DataLib.BLL.Vload vbll = new DataLib.BLL.Vload();
                vmodel = vbll.GetModelByOutID((int)outModel.id);
                if (vmodel != null)
                {
                    txtcutomer.Value = vmodel.inscumname;
                }
            }
            if (inDt != null && inDt.Rows.Count > 0)
            {
                //已有入库单的情况
                inModel = inBll.GetModel(int.Parse(inDt.Rows[0]["id"].ToString()));
                txtRemark.Value = inModel.remark;
                if (HiddenOper.Value == "admin" || txtRemark.Value.Trim() == "")
                {
                    txtRemark.Disabled = false;
                }
                else
                {
                    txtRemark.Disabled = true;
                }
                hidCJOID.Value = inModel.cjoid.ToString();
                hidoutparID.Value = inModel.outid.ToString();

                txtin.Value = inModel.wrlno;
                hidInStoID.Value = inModel.id.ToString();

            }
            else
            {
                GetSerialNum(outModel.outStorageType);
            }

            spanTotal.InnerHtml = outModel.totalWeight.ToString();
            spanCurNwt.InnerHtml = outModel.currentWeight.ToString();
        }

        protected void bindMonth()
        {
            int year = DateTime.Now.Year;

            ListItem listitem = new ListItem((year + 1).ToString() + "年", (year + 1).ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem(year.ToString() + "年", year.ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem((year - 1).ToString() + "年", (year - 1).ToString());
            dropYear.Items.Add(listitem);

            dropYear.SelectedValue = year.ToString();
            dropMonth.SelectedValue = DateTime.Now.Month.ToString().PadLeft(2, '0');


        }

        /// <summary>
        /// 绑定直销页面信息
        /// </summary>
        /// <param name="obj"></param>
        protected void bindZXInfo(int cjoid)
        {
            DataLib.Model.CJOItem citem = new DataLib.BLL.CJOItem().GetListByCid(cjoid);

            DataLib.Model.InStorage inModel = new DataLib.Model.InStorage();
            DataLib.Model.LoadNum loadModel = new DataLib.Model.LoadNum();

            DataLib.BLL.InStorage inBll = new DataLib.BLL.InStorage();
            DataLib.BLL.BarCode barBll = new DataLib.BLL.BarCode();
            DataLib.BLL.OutStorage outBll = new DataLib.BLL.OutStorage();
            DataLib.BLL.LoadNum loadBll = new DataLib.BLL.LoadNum();

            hidoutparID.Value = "0";
            //outModel = outBll.GetModel(int.Parse(outid));
            DataTable inDt = inBll.GetList("cjoid=" + cjoid.ToString()).Tables[0];
            //需要从出库单带出来信息
            inputLoadNum.Value = citem.loadnumno;
            HidVLoad.Value = citem.loadnumno;
            txtSrcOutSid.Value = citem.cjono;
            hidInDepart.Value = citem.Belogcity;
            hidOutDepart.Value = citem.Belogcity;
            hidintype.Value = "Z";
            hidCJOID.Value = citem.cjoid.ToString();
            txtcutomer.Value = citem.companyname;

            hidLoadID.Value = citem.loadnumid.ToString();
            if (inDt != null && inDt.Rows.Count > 0)
            {
                //已有入库单的情况
                inModel = inBll.GetModel(int.Parse(inDt.Rows[0]["id"].ToString()));
                //hidCJOID.Value = inModel.cjoid.ToString();

                txtin.Value = inModel.wrlno;
                hidInStoID.Value = inModel.id.ToString();

            }
            else
            {
                GetSerialNum("7");
            }
            if (HiddenOper.Value == "admin" || txtRemark.Value.Trim() == "")
            {
                txtRemark.Disabled = false;
            }
            else
            {
                txtRemark.Disabled = true;
            }
            spanTotal.InnerHtml = citem.totalWeight.ToString();
            spanCurNwt.InnerHtml = citem.finishWeight.ToString();
        }





    }
}